/*
 * OrderBean.java
 *
 * Created on 12 novembre 2007, 16:13
 */
package com.reimscopie.beans.entities ;

import java.util.* ;

import javax.persistence.* ;

import com.reimscopie.* ;


/**
 * This entity represents an order. This is NOT the cart, but an order already
 * commited.
 */
@Entity
@NamedQueries({
  @NamedQuery(
    name  = "all-orders",
    query = "SELECT o FROM OrderBean o ORDER BY id"
  ),
  @NamedQuery(
    name  = "all-valid-orders",
    query = "SELECT o FROM OrderBean o WHERE valid=true ORDER BY id")
})
public class OrderBean extends ValueBean<Order> {
  
  // commitment date
  private Date date ;
  
  // shipping costs
  private float shipping ;
  
  // order status
  private boolean valid ;
  
  // personal informations of the receiver
  private PersonalInfoBean personalInfo ;
  
  // the list of purchase for this order
  private Collection<PurchaseBean> purchases ;
  
  
  /**
   * Create a new instance of <code>OrderBean</code> (default constructor).
   */
  public OrderBean() {
  }
  
  
  /**
   * Create a new instance of <code>OrderBean</code> with all attributes.
   *
   * @param order The valued order.
   * @param products The products beans.
   */
  public OrderBean(Order order,ProductBean[] products) {
    
    if (order.hasId())
      throw new IllegalArgumentException("order has an image") ;
    
    this.date     = order.getCommitmentDate() ;
    this.shipping = order.getShippingCosts() ;
    this.valid    = order.isValid() ;
    
    this.purchases = new ArrayList<PurchaseBean>() ;
    Purchase[] array = order.getPurchases() ;
    for (int i=0;i<array.length;i++)
      purchases.add(new PurchaseBean(array[i],products[i])) ;
  }
  
  
  /**
   * Return the commitment date for this order.
   *
   * @return The commitment date.
   */
  public Date getDate() {
    return date ;
  }
  
  
  /**
   * Set the commitment date of this order.
   *
   * @param date The new commitment date.
   */
  public void setDate(Date date) {
    this.date = date ;
  }
  
  
  /**
   * Return the shipping costs for this order (money amount).
   *
   * @return The shipping costs.
   */
  public float getShipping() {
    return shipping ;
  }
  
  
  /**
   * Set the shipping costs of this order.
   *
   * @param shipping The new shipping costs.
   */
  public void setShipping(float shipping) {
    this.shipping = shipping ;
  }
  
  
  /**
   * Return the status of this order.
   *
   * @return The order status.
   */
  public boolean getValid() {
    return valid ;
  }
  
  
  /**
   * Set the status of this order.
   *
   * @param status The new order status.
   */
  public void setValid(boolean valid) {
    this.valid = valid ;
  }
  
  
  /**
   * Return the personnalInfo list of this .
   *
   * @return the personal info of the receiver.
   */
  @ManyToOne
  public PersonalInfoBean getPersonalInfo() {
    return personalInfo ;
  }
  
  
  /**
   * Set the personal information of the receiver of this order
   *
   * @param personalInfo The personal information of the receiver
   */
  public void setPersonalInfo(PersonalInfoBean personalInfo) {
    this.personalInfo = personalInfo ;
  }
  
  
  /**
   * Return the purchase list of this order.
   *
   * @return All purchases.
   */
  @OneToMany
  public Collection<PurchaseBean> getPurchases() {
    return purchases ;
  }
  
  
  /**
   * Set the collection of purchases of this order.
   *
   * @param purchases The purchases list of this order.
   */
  public void setPurchases(Collection<PurchaseBean> purchases) {
    this.purchases = purchases ;
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                           TRANSFOMATION                                    //
////////////////////////////////////////////////////////////////////////////////

  /**
   * Transforms this entity bean into the associated order.
   *
   * @return A new order instance.
   *
   * @see #toValue
   */
  public Order toOrder() {
    return toValue() ;
  }

  
  /**
   * Transform this entity bean into the associated value.
   * 
   * @return A new order instance.
   */
  @Override
  public Order toValue() {
    
    PurchaseBean[] beans = new PurchaseBean[purchases.size()] ;
    purchases.toArray(beans) ;
    
    Purchase[] array = new Purchase[purchases.size()] ;
    for (int i=0;i<array.length;i++)
      array[i] = beans[i].toPurchase() ;
    
    
    return new Order(
        getId(),
        array,
        getDate(),
        getShipping(),
        getValid()
    ) ;
    
  }
  
}
